package com.lanou.zy.projecta.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.adapter.MainViewPagerAdapter;
import com.lanou.zy.projecta.application.BaseApplication;
import com.lanou.zy.projecta.fragment.DestinationFragment;
import com.lanou.zy.projecta.fragment.CommunityFragment;
import com.lanou.zy.projecta.fragment.RecommendFragment;
import com.yalantis.contextmenu.lib.ContextMenuDialogFragment;
import com.yalantis.contextmenu.lib.MenuObject;
import com.yalantis.contextmenu.lib.MenuParams;
import com.yalantis.contextmenu.lib.interfaces.OnMenuItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnMenuItemClickListener {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FragmentManager fragmentManager;
    private DialogFragment mMenuDialogFragment;
    private ImageView searchBtn;
    private SharedPreferences sp;
    private AlertDialog ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        fragmentManager = getSupportFragmentManager();
        initToolbar();
        initMenuFragment();
    }

    private void initView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout.setTabMode(tabLayout.MODE_SCROLLABLE);
        tabLayout.setSelectedTabIndicatorColor(0xffffffff);
        tabLayout.setTabTextColors(0xff82d2b6, 0xffffffff);

        searchBtn = (ImageView) findViewById(R.id.searchBtn);
        searchBtn.setOnClickListener(this);

        sp = getSharedPreferences("zy", Context.MODE_PRIVATE);
    }

    // 添加新的页卡
    private void initData() {
        List<String> tabList = new ArrayList<>();
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new RecommendFragment());
        fragmentList.add(new DestinationFragment());
        fragmentList.add(new CommunityFragment());

        tabList.add("推荐");
        tabList.add("目的地");
        tabList.add("社区");

        MainViewPagerAdapter adapter = new MainViewPagerAdapter(getSupportFragmentManager(), fragmentList, tabList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
    }

    private void initToolbar() {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(mToolbar);
    }

    /**
     * 初始化菜单
     *
     * @tool_bar_height 是菜单到屏幕最右边的距离
     */
    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    /**
     * 加载菜单
     */
    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.mipmap.iconfont_cha);

        MenuObject send = new MenuObject("我的主页");
        send.setResource(R.mipmap.iconfont_morentouxiang);

        MenuObject like = new MenuObject("我的头像");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.iconfont_shangchuantouxiang);
        like.setBitmap(b);

        MenuObject addFr = new MenuObject("我的收藏");
        BitmapDrawable bd = new BitmapDrawable(getResources(),
                BitmapFactory.decodeResource(getResources(), R.mipmap.iconfont_shoucangyishoucang));
        addFr.setDrawable(bd);

        MenuObject addFav = new MenuObject("我的好友");
        addFav.setResource(R.mipmap.iconfont_haoyou);

        menuObjects.add(close);
        menuObjects.add(send);
        menuObjects.add(like);
        menuObjects.add(addFr);
        menuObjects.add(addFav);
        return menuObjects;
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.context_menu:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }

                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.searchBtn:
                Intent intent = new Intent(this, SearchActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.right_in, R.anim.left_out);
                break;
        }
    }

    @Override
    public void onMenuItemClick(View v, int position) {
        switch (position) {
            case 1:
//                BaseApplication app = (BaseApplication) getApplication();
                Boolean login = sp.getBoolean("login", false);
//                        (Boolean) app.get("login");
//                Log.d("MainActivity", "login:" + login);

                if (login == false || login == null) {
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(this, UserActivity.class);
                    startActivity(intent);
                }
        }
    }

}
